﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PC_Touch.DataOP
{
    public class SQLiteHelper<T>
    {
        private readonly string _connectionString;

        public SQLiteHelper(string connectionString)
        {
            _connectionString = connectionString;
        }

        public List<T> Query(string query, Func<IDataRecord, T> map)
        {
            var result = new List<T>();

            using (var connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();
                using (var command = new SQLiteCommand(query, connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var item = map(reader);
                            result.Add(item);
                        }
                    }
                }
            }
            return result;
        }
    }
}
